<?php

use console\db\channels\TransferTableMigration;
use yii\db\Query;
use yii\db\Expression;

/**
 * 用户签到信息转移
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m300030_000040_t_signin_record extends TransferTableMigration {

    /**
     * @inheritdoc
     */
    public function buildQuery() {
        return (new Query())->from('{{%' . $this->sourceTable . '}}')
        ->select([
            '*',
            'amount' => new Expression('sum(sign_days)'),
        ])
        ->groupBy('user_id')
        ->orderBy('updated_at desc');
    }

    /**
     * @inheritdoc
     */
    public function getTransferOptions() {
        return [
            'table' => [
                'user_signin' => 'user_signin_history',
            ],
            'columns' => [
                'user_id',
                'amount',
                'serial' => function($data) {
                    return substr_count(decbin($data['history']), '1');
                },
                'last_time' => 'updated_at',
                'recent' => 'history',
                'current_id' => 'id',
                'updated_at',
            ],
        ];
    }

}
